package com.ruoyi.web.controller.mtproject;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.service.IMtProBuildService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.MtContact;
import com.ruoyi.service.IMtContactService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 维保项目合同Controller
 * 
 * @author ruoyi
 * @date 2020-07-04
 */
@Controller
@RequestMapping("/ruoyi/contact")
public class MtContactController extends BaseController
{
    private String prefix = "ruoyi/contact";

    @Autowired
    private IMtContactService mtContactService;
    private IMtProBuildService mtProBuildService;

    @RequiresPermissions("ruoyi:contact:view")
    @GetMapping("/contact/{mtId}")
    public String contact(@PathVariable("mtId") Long mtId ,ModelMap mmap)
    {
        mmap.put("mtId",mtId);
        return prefix + "/contact";
    }

    /**
     * 查询维保项目合同列表
     */
    @RequiresPermissions("ruoyi:contact:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MtContact mtContact)
    {
        startPage();
        List<MtContact> list = mtContactService.selectMtContactList(mtContact);
        return getDataTable(list);
    }

    /**
     * 按Id查询维保项目合同列表
     */
    @RequiresPermissions("ruoyi:contact:list")
    @PostMapping("/lists/{mtId}")
    @ResponseBody
    public TableDataInfo lists(@PathVariable("mtId") Long mtId)
    {
        startPage();
        List<MtContact> lists = mtContactService.selectMtContactListById(mtId);
        return getDataTable(lists);
    }

    /**
     * 导出维保项目合同列表
     */
    @RequiresPermissions("ruoyi:contact:export")
    @Log(title = "维保项目合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{mtId}")
    @ResponseBody
    public AjaxResult export(@PathVariable("mtId") Long mtId,MtContact mtContact)
    {
        mtContact.setMtId(mtId);
        List<MtContact> list = mtContactService.selectMtContactList(mtContact);
        ExcelUtil<MtContact> util = new ExcelUtil<MtContact>(MtContact.class);
        return util.exportExcel(list, "维保项目合同");
    }

    /**
     * 新增维保项目合同
     */
    @GetMapping("/add/{mtId}")
    public String add(@PathVariable("mtId") Long mtId,ModelMap mmap )
    {
        mmap.put("mtId",mtId);
        return prefix + "/add";
    }

    /**
     * 新增保存维保项目合同
     */
    @RequiresPermissions("ruoyi:contact:add")
    @Log(title = "维保项目合同", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file,
                              @RequestParam("file1") MultipartFile file1,
                              MtContact mtContact) throws IOException
    {
        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        String fileName1 = FileUploadUtils.upload(filePath, file1);
        mtContact.setMtContractUpload(fileName);
        mtContact.setMtInvoiceUpload(fileName1);
        return toAjax(mtContactService.insertMtContact(mtContact));
    }

    /**
     * 新增保存维保项目合同
     */
    @RequiresPermissions("ruoyi:contact:add")
    @Log(title = "维保项目合同", businessType = BusinessType.INSERT)
    @PostMapping("/addone")
    @ResponseBody
    public AjaxResult addSaves(@RequestParam("file") MultipartFile file,
                              MtContact mtContact) throws IOException
    {
        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        mtContact.setMtContractUpload(fileName);
        return toAjax(mtContactService.insertMtContact(mtContact));
    }
    /**
     * 修改维保项目合同
     */
    @GetMapping("/edit/{mtContractId}")
    public String edit(@PathVariable("mtContractId") Long mtContractId, ModelMap mmap)
    {
        MtContact mtContact = mtContactService.selectMtContactById(mtContractId);
        mmap.put("mtContact", mtContact);
        return prefix + "/edit";
    }

    /**
     * 修改保存维保项目合同
     */
    @RequiresPermissions("ruoyi:contact:edit")
    @Log(title = "维保项目合同", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam("file1") MultipartFile file1,MtContact mtContact)throws IOException
    {
            // 上传文件路径
            String filePath = Global.getUploadPath();
            // 上传并返回新文件名称
            String fileName1 = FileUploadUtils.upload(filePath, file1);
            mtContact.setMtInvoiceUpload(fileName1);
        return toAjax(mtContactService.updateMtContact(mtContact));
    }

    /**
     * 删除维保项目合同
     */
    @RequiresPermissions("ruoyi:contact:remove")
    @Log(title = "维保项目合同", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mtContactService.deleteMtContactByIds(ids));
    }

    /**
     *
     * 下载功能
     */
    @GetMapping("downloadFile")
    public void resourceDownload(@PathVariable("pUploading") String fAccessory, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 本地资源路径
        String localPath = Global.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(fAccessory, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }
}
